<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Order extends MY_Controller
{
    /**
     * Index Page for this controller.
     */
    public function index()
    {
//        require_once dirname(dirname(__DIR__)).'/public/wxpay/example/WxPay.JsApiPay.php';.
        $orderHeader = $this->input->get_post('orderHeader');
        $orderBody = $this->input->get_post('orderBody');
        $dataList = $this->input->get_post('dataList');
        $userInfo = $this->input->get_post('userInfo');
        $payTypeList = $this->input->get_post('payTypeList');

        if (!$orderHeader) {
            redirect(WEBROOT . '/cart/index');
        }

        if ($orderHeader) {
            $this->data['orderHeader'] = json_decode($orderHeader, true);
        }
        if ($orderBody) {
            $this->data['orderBody'] = json_decode($orderBody, true);
        }
        if ($dataList) {
            $this->data['dataList'] = json_decode($dataList, true);
        }
        if ($userInfo) {
            $this->data['userInfo'] = json_decode($userInfo, true);
        }
        if ($payTypeList) {
            $this->data['payTypeList'] = json_decode($payTypeList, true);
        }
        $cart = array();
        if ($this->data['orderBody']) {
            foreach ($this->data['orderBody'] as $val) {
                $cart[] = $val['product_id'] . '_' . $val['product_sku_id'];
            }
        }
        if (isset($this->data['orderHeader']['status']) && $this->data['orderHeader']['status'] == 1) {
            redirect(WEBROOT . '/cart/index');
        }
        $this->data['cart'] = implode('#', $cart);
//        $this->data['openid'] = $openId;
        $this->render('order/index', $this->data);
    }

    public function success()
    {
        $id = $this->input->get_post('id');
//        $openid = $this->input->get('openid')?$this->input->get('openid'):'o5GoWs7wwOz1-vc0oQXtlo1W8jgY';
        $this->load->library('jsapipay');
        $openid = $this->jsapipay->GetOpenid();
//        $openid = 'o5GoWs7wwOz1-vc0oQXtlo1W8jgY';
        if($id){
            $this->load->library('C_weixinpay');
            $sendData = array();
            $sendData['order_id'] = $id;
            log_message('error','order--'.$id);
            $sendData['password'] = $this->lg_user['password'];
            $sendData['uid'] = $this->lg_user['id'];
            $sendData['enterpriseId'] = $this->lg_user['enterpriseId'];
            $sendData['channelId'] = $this->lg_user['channelId'];
            $sendData['userType'] = $this->lg_user['type'];
            $sendData['openid'] = $openid;
            //制造接口请求
            $url = $this->c_weixinpay->set_call_items($sendData);
            $param = $this->c_weixinpay->get_call_items();
            $rs = $this->restcurl->put($url, $param);
            log_message('error','rs---'.json_encode($rs));
            $rs['CallInfo']['rsa']['paySign'] = $rs['CallInfo']['rsa']['sign'];
            unset($rs['CallInfo']['rsa']['sign']);
            $this->data['info'] = $rs['CallInfo']['rsa'];
        }
        $this->data['id'] = $id;
        $this->render('order/success', $this->data);
    }
    public function wxnotify()
    {
        $this->load->library('C_postpay');
        $sendData = array();
        $sendData['order_id'] = $this->input->get('order_id');
        $sendData['password'] = $this->lg_user['password'];
        $sendData['uid'] = $this->lg_user['id'];
        $sendData['enterpriseId'] = $this->lg_user['enterpriseId'];
        $sendData['channelId'] = $this->lg_user['channelId'];
        $sendData['userType'] = $this->lg_user['type'];
        //制造接口请求
        $url = $this->c_postpay->set_call_items($sendData);
        $param = $this->c_postpay->get_call_items();
        $rs = $this->restcurl->put($url, $param);
        $result['msg'] = $rs;
       echo json_encode($result);die;
        $rs['CallInfo']['rsa']['paySign'] = $rs['CallInfo']['rsa']['sign'];
        unset($rs['CallInfo']['rsa']['sign']);
        $this->data['info'] = $rs['CallInfo']['rsa'];
    }
}
